package com.webSite.demo.cy.ocr;

import java.io.File;
import java.util.HashMap;
import java.util.List;

import javax.swing.filechooser.FileSystemView;

import cn.hutool.core.io.file.FileWriter;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;

public class OcrFile {

	private static final String ocrPath = "http://ocr.shouji.sogou.com/v2/ocr/json";
	private static String writeSrc;
	
	static {
		FileSystemView fsv = FileSystemView.getFileSystemView();
		File com=fsv.getHomeDirectory();    //这便是读取桌面路径的方法了
		writeSrc = com.getPath();
	}
	
	/**
	 * 构造
	* @Title: ocrImage  
	* @param @param srcFile    参数  
	* @return void    返回类型  
	* @throws
	 */
	public void ocrImage(String srcFile) {
		ocrImage(new File(srcFile),"自动识别");
	}
	
	/**
	 * 构造
	* @Title: ocrImage  
	* @param @param srcFile
	* @param @param txtName    参数  文件名称
	* @return void    返回类型  
	* @throws
	 */
	public void ocrImage(String srcFile,String txtName) {
		ocrImage(new File(srcFile),txtName);
	}
	
	/**
	 * 构造
	* @Title: ListOcrImage  
	* @param @param srcFile
	* @param @param txtName    参数  
	* @return void    返回类型  
	* @throws
	 */
	public void ListOcrImage(String srcFile,String txtName) {
		ListOcrImage(new File(srcFile),txtName,new File(writeSrc));
	}
	
	/**
	 * 构造
	* @Title: ListOcrImage  
	* @param @param srcFile
	* @param @param txtName    参数  
	* @return void    返回类型  
	* @throws
	 */
	public void ListOcrImage(String srcFile,String txtName,String outSrc) {
		ListOcrImage(new File(srcFile),txtName,new File(outSrc));
	}
	
	
	/**
	 * 构造
	* @Title: ListOcrImage  
	* @param @param srcFile
	* @param @param txtName    参数  
	* @return void    返回类型  
	* @throws
	 */
	public void ListOcrImage(File srcFile,String txtName,File outSrc) {
		//如果目录不存在则创建
		if(!outSrc.exists() && outSrc.isDirectory()) {
			outSrc.mkdir();
		}
		StringBuilder build = new StringBuilder();
		
		File[] newFileArray = getFileArrayDesc(srcFile);
		
		for(File f:newFileArray) {
			System.out.println(f.getName());
			build.append(ocrImageToString(f));
		}
		
		FileWriter fileWriter = new FileWriter(outSrc.getAbsolutePath()+"\\"+txtName+".txt");
		fileWriter.write(build.toString());
	}
	

	/**
	 * 批量识别ocr
	* @Title: ListOcrImage  
	* @param @param srcFile
	* @param @param txtName    参数  
	* @return void    返回类型  
	* @throws
	 */
	public void ListOcrImage(List<String> srcFile,String txtName) {
		StringBuilder build = new StringBuilder();
		
		for (String string : srcFile) {
			build.append(ocrImageToString(new File(string)));
		}
		
		FileWriter fileWriter = new FileWriter(writeSrc+"\\"+txtName+".txt");
		fileWriter.write(build.toString());
	}
	
	/**
	 * 获取顺序下的file文件夹目录
	* @Title: getFileArray  
	* @param @param srcFile
	* @param @return    参数  
	* @return File[]    返回类型  
	* @throws
	 */
	private File[] getFileArrayDesc(File srcFile) {
		File[] file = srcFile.listFiles();
		File first = file[0];
		
		int size = file.length;
		File[] newFileArray = new File[size];
		
		if(first.getName().contains("(")) {
			String name = first.getName().substring(0, first.getName().indexOf("("));
			for(int i=0;i<size;i++) {
				newFileArray[i] = new File(srcFile.getAbsolutePath()+"\\"+name+"("+Integer.valueOf(i+1)+")"+".jpg");
			}
		}else {
			for(int i=0;i<size;i++) {
				newFileArray[i] = new File(srcFile.getAbsolutePath()+"\\"+i+".jpg");
			}
		}
		
		return newFileArray;
	}
	
	/**
	 * 输出string
	* @Title: ocrImageToString  
	* @param @param srcFile
	* @param @return    参数  
	* @return String    返回类型  
	* @throws
	 */
	public String ocrImageToString(String srcFile) {
		return ocrImageToString(new File(srcFile)).toString();
	}
	
	/**
	 * 返回StringBuilder对象
	* @Title: ocrImageToString  
	* @param @param srcFile
	* @param @return    参数  
	* @return StringBuilder    返回类型  
	* @throws
	 */
	private StringBuilder ocrImageToString(File srcFile) {
		HashMap<String, Object> paramMap = new HashMap<>();
		paramMap.put("pic", srcFile);
		
		String result = HttpUtil.post(ocrPath, paramMap);
		OcrApi api = JSONUtil.toBean(result, OcrApi.class);
		
		StringBuilder builder = new StringBuilder();
		for (OcrResult ocrResult : api.getResult()) {
			builder.append(ocrResult.getContent()+"\r\n");
		}
		
		return builder;
	}
	
	/**
	 * 图文识别接口(此方法主要针对单张图片)
	* @Title: ocrImage  
	* @param @param srcFile 需要识别的文件路径
	* @param @param txtName    参数  输出的文件名称
	* @return void    返回类型  
	* @throws
	 */
	private void ocrImage(File srcFile,String txtName) {
		HashMap<String, Object> paramMap = new HashMap<>();
		paramMap.put("pic", srcFile);
		
		String result = HttpUtil.post(ocrPath, paramMap);
		OcrApi api = JSONUtil.toBean(result, OcrApi.class);
		
		StringBuilder builder = new StringBuilder();
		for (OcrResult ocrResult : api.getResult()) {
			builder.append(ocrResult.getContent()+"\r\n");
		}
		
		FileWriter fileWriter = new FileWriter(writeSrc+"\\"+txtName+".txt");
		fileWriter.write(builder.toString());
	}
	
	
}
